package com.maple.myormsqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.maple.myormsqlite.bean.Person;
import com.maple.myormsqlite.bean.User;
import com.maple.myormsqlite.bean.UserImg;
import com.maple.myormsqlite.db.BaseDao;
import com.maple.myormsqlite.db.BaseDaoFactory;
import com.maple.myormsqlite.db.BaseDaoNewImpl;
import com.maple.myormsqlite.db.PhotoDao;
import com.maple.myormsqlite.sub_sqlite.BaseDaoSubFactory;
import com.maple.myormsqlite.sub_sqlite.UserDao;
import com.maple.myormsqlite.update.UpdateManager;

import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private int i = 0;
    private UpdateManager updateManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        updateManager = new UpdateManager();
    }

    public void insert(View view) {
        BaseDao baseDao = BaseDaoFactory.getOurInstance().getBaseDao(User.class);
        baseDao.insert(new User("1", "maple", "123"));

        BaseDao personBaseDao = BaseDaoFactory.getOurInstance().getBaseDao(Person.class);
        personBaseDao.insert(new Person(2, "maple_person", "123"));

        Toast.makeText(this, "执行成功！", Toast.LENGTH_SHORT).show();
    }

    public void clickUpdate(View view) {
        BaseDaoNewImpl baseDao = BaseDaoFactory.getOurInstance().getBaseDao(
                BaseDaoNewImpl.class, Person.class);
        Person person = new Person();
        person.setName("jett");
        Person where = new Person();
        where.setId(1);
        long update = baseDao.update(person, where);
        Toast.makeText(this, "执行成功！", Toast.LENGTH_SHORT).show();
    }

    public void clickDelete(View view) {
        BaseDaoNewImpl baseDao = BaseDaoFactory.getOurInstance().getBaseDao(
                BaseDaoNewImpl.class, Person.class);
        Person where = new Person();
        where.setName("jett");
        where.setId(1);
        int delete = baseDao.delete(where);
        Toast.makeText(this, "执行成功！", Toast.LENGTH_SHORT).show();
    }

    public void clickSelect(View view) {
        BaseDaoNewImpl baseDao = BaseDaoFactory.getOurInstance().getBaseDao(
                BaseDaoNewImpl.class, Person.class);
        Person where = new Person();
        where.setId(1);
        List<Person> query = baseDao.query(where);
        Log.i("maple", query.size() + "");
    }

    public void clickLogin(View view) {
        User user = new User();
        user.setName("maple" + (++i));  //maple1  maple2  maple3
        user.setPassword("123456");
        user.setId("N00" + i);   //N001  N002  N003_login.db
        UserDao userDao = BaseDaoFactory.getOurInstance().getBaseDao(UserDao.class, User.class);
        userDao.insert(user);
        Toast.makeText(this, "执行成功！", Toast.LENGTH_SHORT).show();
    }

    public void clickSubInsert(View view) {
        UserImg img = new UserImg();
        img.setTime(new Date().toString());
        img.setImgPath("www.baidu.com/123123123.png");

        PhotoDao subDao = BaseDaoSubFactory.getInstance().getSubDao(PhotoDao.class, UserImg.class);
        subDao.insert(img);
        Toast.makeText(this, "执行成功！", Toast.LENGTH_SHORT).show();
    }

    public void write(View view) {
        updateManager.saveVersionInfo(this, "V003");
    }

    public void update(View view) {
        updateManager.checkThisVersionTable(this);
        updateManager.startUpdateDb(this);
    }
}
